function saturdayColors(month){
    var myDate = new Date();
    myDate.setMonth(month);
    myDate.setDate(1);
    var y = myDate.getFullYear;
    var curr = myDate.getDay();
    
    var maxDay;
    if(month == 1){
        maxDay =  (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28;
    }
    else if (month == 3 || month == 5 || month == 8 || month == 10)
        maxDay = 30;
    else
        maxDay = 31;
        
    var daysArr = new Array();
    for(var i = 0; i < maxDay; i++){
        if(curr == 6)
            daysArr[i] = true;
        else
            daysArr[i] = false;
        
        curr += 1;
        curr = curr % 7;
    }
    
    return daysArr;
//    console.log(daysArr);
}
function buildRow(child_att, daysArr){
    var child = JSON.parse(child_att[0]);
    var row = '<tr><td class="active">' + child.first_name + ' ' + child.last_name+ '</td>\
    <td class="active">' + child.child_id + '</td>';
    var att = '';
    for(var i = 1; i < (daysArr.length + 1); i++){
        if(daysArr[i-1]){
            row += '<td class="active"></td>';
        }
        else{
            att = (child_att[i])? '<i class="fa fa-check"></i>' :'<i class="fa fa-times"></i>';
            row += '<td>' + att + '</td>';
        }
    }
    row += '</tr>';
    
    return row;
}

function buildReport(children, month){
    var daysArr = saturdayColors(month);
    var maxDay = daysArr.length;
    var openDiv = '<div class="row">\
                    <div class="table-responsive">\
                        <table class="table table-bordered table-hover">';
    
    var headRow = '<thead><tr>';
    headRow += '<th class="text-right">שם</th>\
    <th class="text-right">ת.ז</th>'
    for(var i = 0; i < maxDay; i++)
        headRow += '<th class="text-right">' + (i + 1) + '</th>';
        
    headRow += '</tr></thead>';
    
    var bodyCont = '<tbody>';
    for (var i = 0; i < children.length; i++) {
            bodyCont += buildRow(children[i], daysArr);
    //        console.log(children[i]);
    }
    
    bodyCont += '</tbody>';
    
    var closeDiv = '</table>\
                </div>\
            </div>\
        </div>';
    
    return (openDiv + headRow + bodyCont + closeDiv);
}
    

function showReport(data){
    var children = JSON.parse(JSON.stringify(data));
    var month = document.getElementById("options").selectedIndex;
    
    console.log(data);
    
    if (data.length == 0){
        console.log('no reports');
        var temp = '<div class="alert alert-danger"> לא קיימים ערכים לחודש המבוקש</div>';
        $('#forAnnouncement').html(temp);
        $('#printBTN').removeClass('btn-info').addClass('btn-default disabled');
        return;
    }
    else{
        console.log('should display report');
        $('#forAnnouncement').html('');
        var report = buildReport(children, month);
        $('#reports').html(report);
        $('#printBTN').addClass('btn-info').removeClass('btn-default disabled');
    }
}

function getReport(){
    var month = document.getElementById("options").selectedIndex;
    var temp = '<div class="alert alert-info">\
                    <i class="fa fa-spinner fa-spin"></i> הדו"ח נטען, נא להמתין</div>';
    $('#forAnnouncement').html(temp);
    $('#reports').html('');
    
    $.ajax('/report', {
        type: 'GET',
        data: { 
            'fmt': 'json',
            'month': month
        },
        
        success: showReport
    });
}


$(document).ready(function(){
    var d = new Date();
    $('select[name="options"]').find('option[value="'+ d.getMonth() + '"]').attr("selected",true);
})

